﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yang.DataBase;

public partial class WebServices_getMachineDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
        Response.Charset = "utf-8";
        string resultJson = "{ \"Details\":[{";
        //int nMachines = 0;

        string machineCode = Request.QueryString[0].ToString();
        

        string toolCode = string.Empty;
        double JobRation = 0.0;
        try
        {
            string strSql = "select 牌照号,所有人姓名,所属单位,机车类型 from 农机基本信息 where 牌照号 = '" + machineCode + "'";

            DataSet ds = Database.Query(strSql);
            if (ds != null)
            {
                resultJson += "\"MachineCode\":\"" + ds.Tables[0].Rows[0][0].ToString() + "\",";
                resultJson += "\"MachineOwner\":\"" + ds.Tables[0].Rows[0][1].ToString() + "\",";
                resultJson += "\"MachineDept\":\"" + ds.Tables[0].Rows[0][2].ToString() + "\",";
                resultJson += "\"MachineType\":\"" + ds.Tables[0].Rows[0][3].ToString() + "\",";
            }
            else
            {
                Log.WriteException("未在 农机基本信息表 中查找到对应农机编号");
                resultJson = "{\"Details\":\"Error\"}";
            }




            strSql = "select 驾驶员姓名,联系电话,驾驶证号,准驾机型,编号 from 驾驶员信息 where 驾驶农机 = '" + machineCode + "'";
            ds.Clear();
            ds = Database.Query(strSql);
            if (ds != null)
            {
                resultJson += "\"DriverName\":\"" + ds.Tables[0].Rows[0][0].ToString() + "\",";
                resultJson += "\"DriverPhone\":\"" + ds.Tables[0].Rows[0][1].ToString() + "\",";
                resultJson += "\"DriverCode\":\"" + ds.Tables[0].Rows[0][2].ToString() + "\",";
                resultJson += "\"DriverType\":\"" + ds.Tables[0].Rows[0][3].ToString() + "\",";
            }
            else
            {
                Log.WriteException("未在 驾驶员信息 中查找到对应驾驶员");
                resultJson = "{\"Details\":\"Error\"}";
            }




            strSql = "select 农具编码,农具种类名称,所有人姓名,所属单位 from 农具基本信息 where 农机牌照号 = '" + machineCode + "'";
            ds.Clear();
            ds = Database.Query(strSql);
            if (ds != null)
            {
                resultJson += "\"ToolCode\":\"" + ds.Tables[0].Rows[0][0].ToString() + "\",";
                resultJson += "\"ToolCatalog\":\"" + ds.Tables[0].Rows[0][1].ToString() + "\",";
                resultJson += "\"ToolOwner\":\"" + ds.Tables[0].Rows[0][2].ToString() + "\",";
                resultJson += "\"ToolDept\":\"" + ds.Tables[0].Rows[0][3].ToString() + "\",";
                toolCode = ds.Tables[0].Rows[0][0].ToString();
            }
            else
            {
                Log.WriteException("未在 农具基本信息 中查找到对应农具");
                resultJson = "{\"Details\":\"Error\"}";
            }


            //作业名称	 技术要求	 年度	 牵引农具	 折合系数
            double JobPrice = 0.0;
            
            strSql = "select 作业编码,作业名称,牵引农具,折合系数,金额 from 作业种类 where 牵引农具编码 = '" + ds.Tables[0].Rows[0][0].ToString() + "'";
            ds.Clear();
            ds = Database.Query(strSql);
            if (ds != null)
            {
                resultJson += "\"JobName\":\"" + ds.Tables[0].Rows[0][1].ToString() + "\",";
                resultJson += "\"JobTool\":\"" + ds.Tables[0].Rows[0][2].ToString() + "\",";
                resultJson += "\"JobRatio\":\"" + ds.Tables[0].Rows[0][3].ToString() + "\",";
                //resultJson += "\"txtJobAmount\":\"" + ds.Tables[0].Rows[0][3].ToString() + "\",";
                //JobCode = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                JobRation = double.Parse(ds.Tables[0].Rows[0][3].ToString());
                JobPrice = double.Parse(ds.Tables[0].Rows[0][4].ToString());
            }
            else
            {
                Log.WriteException("未在 农具基本信息 中查找到对应农具");
                resultJson = "{\"Details\":\"Error\"}";
            }


            
            string strDate = DateTime.Now.Date.ToString();
            strSql = "select Latitude,Longitude,WorkStatus,WorkWidth,WorkCode from GpsInfo where LocationTime > '" + strDate + "' and DeviceCode = '" + machineCode + "' order by LocationTime DESC";
            ds.Clear();
            ds = Database.Query(strSql);
            double x1, x2, y1, y2;
            //double GpsRouteLength = 0.0;
            int JobCode = 0;
            double WorkWidth = 0.0;
            double WorkArea = 0.0;
            AgriMachineWork work = new AgriMachineWork();
            work.WorkCode = JobCode;
            work.WorkPrice = JobPrice;
            work.WorkRatio = JobRation;



            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++)
                {
                    if (int.Parse(ds.Tables[0].Rows[i][2].ToString()) == 1)//判断当前农机是否处于作业状态，并且只计算最后一个作业类型的工作量。
                    {
                        y1 = double.Parse(ds.Tables[0].Rows[i][0].ToString());
                        x1 = double.Parse(ds.Tables[0].Rows[i][1].ToString());
                        y2 = double.Parse(ds.Tables[0].Rows[i + 1][0].ToString());
                        x2 = double.Parse(ds.Tables[0].Rows[i + 1][1].ToString());
                        WorkWidth = (double.Parse(ds.Tables[0].Rows[i + 1][3].ToString()) + double.Parse(ds.Tables[0].Rows[i][3].ToString()))/2;
                        //GpsRouteLength += GetDistance(y1, x1, y2, x2);// *WorkWidth;
                        WorkArea += GetDistance(y1, x1, y2, x2) * WorkWidth;
                    }
                    else
                    {
                        break;
                    }
                }
                work.WorkArea = WorkArea;
                work.WorkCode = int.Parse(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][4].ToString());
            }
            else
            {
                Log.WriteInfo("农机未开启");
                resultJson = "{\"Details\":\"Error\"}";
            }

            //strSql = "select 幅宽 from 农具种类 where 农具编码 = '" + toolCode + "'";
            //ds.Clear();
            //ds = Database.Query(strSql);

            //if (ds != null)
            //{
            //    double JobWidth = double.Parse(ds.Tables[0].Rows[0][0].ToString());

            double JobAmount = Math.Round(WorkArea * JobRation / 666.67, 2);
            double JobMoney = Math.Round(JobAmount * JobPrice,2);
            string strJobAmount = JobAmount.ToString();

            work.setWorkPriceAndRatio(work.WorkCode);
            resultJson += "\"JobAmount\":\"" + work.WorkAmount.ToString() + "亩(" + work.WorkMoney.ToString() + "元)\",";
            //}
            //else
            //{
            //    Log.WriteException("未在 农具种类 中查找到对应农具编码");
            //    resultJson = "{\"Details\":\"Error\"}";
            //}
            if (resultJson.IndexOf("Error") < 0)
            {
                resultJson = resultJson.Substring(0, resultJson.Length - 1);
                resultJson += "}]}";
            }
        }
        catch (Exception ex)
        {
            Log.WriteException(DateTime.Now.ToString() + ":" + ex.Message.ToString() + "\n");
        }

        Response.Write(resultJson);
    }




    private const double EARTH_RADIUS = 6378137.0;//地球半径

    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }
    public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
    {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
         Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.Round(s * 10000) / 10000;
        return s;
    }
}
